package com.mall.controller;



import com.mall.pojo.Active;
import com.mall.pojo.Admin;
import com.mall.pojo.Business;
import com.mall.pojo.Good;
import com.mall.service.AdminService;
import com.mall.util.JWTUtil;
import com.mall.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;



    @RequestMapping("/back/staff/login")
    public Result login(String workNum,String password){
        System.out.println(workNum);
        System.out.println(password);
        System.out.println("管理员登录");
        Admin admin = adminService.login(workNum,password);
        System.out.println(admin);
        Result result = new Result();


        if (admin == null) {
            result.setCode(0);
            result.setMessage("账号密码有误");
        } else {
            result.setCode(1);
            result.setMessage("管理员登录成功！");
            result.getDataMap().put("admin", admin);

        }
        //生成token
        if (admin != null) {
            String token = JWTUtil.createToken(""+admin.getAdminId(),admin.getAdminName(),null);
            System.out.println(token+"-0-------");
            result.getDataMap().put("token",token);
        }
        return result;  //响应给前端的内容

    }






    @RequestMapping("/back/user/showGoodPlus")
    public Result showGoodPlus(String name,Integer pageSize, Integer offset){
        System.out.println(name);
        System.out.println(pageSize);
        System.out.println(offset);
        System.out.println("展示");


        List<Good> goodList = adminService.showGoodPlus(name,pageSize,offset);
        System.out.println(goodList);

        Integer totalSearch =  adminService.searchCountGood(name);
        System.out.println(totalSearch+"----");
        List<Good> goodAllList =  adminService.searchAll();
        System.out.println(goodAllList);

        Result result = new Result();

        result.setCode(1);
        result.setMessage("搜索成功");
        result.getDataMap().put("goodList", goodList);
        result.getDataMap().put("totalSearch", totalSearch);
        result.getDataMap().put("goodAllList", goodAllList);
        return result;  //响应给前端的内容
    }




    @RequestMapping("/back/user/goClassify")
    public Result goClassify(Integer goodId,Integer pageSize, Integer offset){
        System.out.println(goodId);
        System.out.println(pageSize);
        System.out.println(offset);
        System.out.println("展示");


        List<Good> goodList = adminService.showGoodClassify(goodId,pageSize,offset);
        System.out.println(goodList);

        Integer totalSearch =  adminService.showGoodClassifyCount(goodId);
        System.out.println(totalSearch+"----");





        Result result = new Result();

        result.setCode(1);
        result.setMessage("搜索成功");
        result.getDataMap().put("goodList", goodList);

        result.getDataMap().put("totalSearch", totalSearch);

        return result;  //响应给前端的内容
    }




    @RequestMapping("/back/user/backToPre")
    public Result backToPre(Integer pid,Integer pageSize, Integer offset){
        System.out.println(pid);
        System.out.println(pageSize);
        System.out.println(offset);
        System.out.println("返回上一级");


        Integer currentPid = adminService.backToPre(pid);
        System.out.println(currentPid+"-----当前pid 在controller");

        List<Good> goodList=  adminService.selectPreById(currentPid);
        System.out.println(goodList+"在service层");

        Integer totalSearch =  adminService.showGoodClassifyCount(currentPid);
        System.out.println("byId的总数是"+totalSearch);
        Result result = new Result();

        result.setCode(1);
        result.setMessage("搜索成功");
        result.getDataMap().put("goodList", goodList);
        result.getDataMap().put("totalSearch", totalSearch);

        return result;  //响应给前端的内容
    }






    @RequestMapping("/back/user/sureOurAdd")
    public Result sureOurAdd(Integer pid,String goodName,String level){
        System.out.println(pid);
        System.out.println(goodName);
        System.out.println(level);



        Boolean res = adminService.insertByPid(pid,goodName,level);



        //alt  回车  生成方法！
        Result result = new Result();
        if (res) {
            result.setCode(1);
            result.setMessage("新增成功");
        } else {
            result.setCode(0);
            result.setMessage("新增失败");

        }


        return result;  //响应给前端的内容
    }





    @RequestMapping("/back/admin/addKill")
    public Result addKill(String activeName,String startTime,String endTime){
        System.out.println(activeName);
        System.out.println(startTime);
        System.out.println(endTime);


        Active active = new Active(null,activeName,startTime,endTime,1);



        Boolean res = adminService.addKill(active);



        //alt  回车  生成方法！
        Result result = new Result();
        if (res) {
            result.setCode(1);
            result.setMessage("新增成功");
        } else {
            result.setCode(0);
            result.setMessage("新增失败");

        }


        return result;  //响应给前端的内容
    }




    @RequestMapping("/back/admin/addNext")
    public Result addNext(Integer Gid,String goodName){
        System.out.println(Gid);
        System.out.println(goodName);




        String leverId = adminService.selectLevelByGId(Gid);
        System.out.println("获得的levelId是"+leverId+"===================");


       Boolean res = adminService.insertByPid(Gid,goodName,leverId);


        //alt  回车  生成方法！
        Result result = new Result();
        if (res) {
            result.setCode(1);
            result.setMessage("新增下一类成功");
        } else {
            result.setCode(0);
            result.setMessage("新增下一类失败");

        }


        return result;  //响应给前端的内容
    }





    @RequestMapping("/back/admin/showStorePlus")
    public Result showStorePlus(String name,String status,  Integer pageSize, Integer offset){
        System.out.println(name);
        System.out.println(status+"----status-");
        System.out.println(pageSize+"--"+offset);



        List<Business> businessList = adminService.showStorePlus(name,status,pageSize,offset);
        System.out.println(businessList);

        Integer totalSearch =  adminService.showStorePlusCount(name,status);
        System.out.println(totalSearch+"----");


        Result result = new Result();

        result.setCode(1);
        result.setMessage("搜索成功");
        result.getDataMap().put("businessList", businessList);
        result.getDataMap().put("totalSearch", totalSearch);

        return result;
    }




    @RequestMapping("/back/admin/showKillActive")
    public Result showKillActive(String name ,Integer pageSize, Integer offset){
        System.out.println(name);
        System.out.println(pageSize+"--"+offset);



        List<Active> activeList = adminService.showKillActive(name,pageSize,offset);
        System.out.println(activeList);

        Integer totalSearch =  adminService.showKillActiveCount(name);
        System.out.println(totalSearch+"----");


        Result result = new Result();

        result.setCode(1);
        result.setMessage("秒杀活动搜索成功");
        result.getDataMap().put("activeList", activeList);
        result.getDataMap().put("totalSearch", totalSearch);

        return result;
    }










    @RequestMapping("/back/admin/passReview")
    public Result passReview(Integer businessId){
        System.out.println(businessId+"------过来了");
        Boolean res = adminService.passToOne(businessId);

        //alt  回车  生成方法！
        Result result = new Result();
        if (res) {
            result.setCode(1);
            result.setMessage("修改状态为1成功");
        } else {
            result.setCode(0);
            result.setMessage("修改状态为2成功");

        }
        return  result;
    }

}
